package com.bdqn.ch02.SZ.sz2;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * DOMAddUpdDel
 *
 * @aurhor Administrator  whs
 * @since 2024/7/1
 */
public class DOMAddUpdDel {
    public static void main(String[] args) {
        // 得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {

            //  从 DOM 工厂获得 DOM 解析器
            DocumentBuilder db = dbf.newDocumentBuilder();
            //  解析XML文档 得到一个 Document 即DOM 树
            Document doc = db.parse("src/com/bdqn/ch02/SZ/sz1/手机品牌.xml");
            //  首先 创建Brand 节点
            Element brandElement = doc.createElement("Brand");


//            1.添加三星品牌

            brandElement.setAttribute("name","三星");
            //  然后 创建 Type 节点
            Element typeElement = doc.createElement("Type");
            typeElement.setAttribute("name","NoteX");
            System.out.println("添加三星品牌成功！");


//           2。修改三星品牌

            // 找到修改的 Brand 节点
            NodeList list = doc.getElementsByTagName("Brand");

            for (int i = 0; i < list.getLength(); i++) {
                String brandName = brandElement.getAttribute("name");
                // 判断如果值为三星，则进行修改
                if (brandName.equals("三星")){
                    brandElement.setAttribute("name","SAMSUNG"); //  找到name属性为三星的节点 并修改属性的值
                    System.out.println("修改三星品牌成功！");
                }

            }


//            3.删除三星品牌

            NodeList lists = doc.getElementsByTagName("Brand");
            for (int i = 0; i < lists.getLength(); i++) {
                Element brandElements = (Element)list.item(i);
                String brandName = brandElements.getAttribute("name");
                // 判断如果值为 SAMSUNG，则进行删除
                if (brandName.equals("SAMSUNG")){

                    brandElements.getParentNode().removeChild(brandElements);  // 先找到父节点 然后 删除这个子节点
                    System.out.println("删除SAMSUNG品牌成功！");
                }

            }





            //  最后 添加子父关系
            brandElement.appendChild(typeElement);
            Element phoneElement = (Element)doc.getElementsByTagName("PhoneInfo").item(0);
            phoneElement.appendChild(brandElement);

            // 保存 XML 文件
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource domSource = new DOMSource(doc);




            // 设置编码类型
            transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
            StreamResult result = new StreamResult(new FileOutputStream("src/com/bdqn/ch02/SZ/sz1/手机品牌.xml"));


            //  把 DOM 树转换为 XML 文件
            transformer.transform(domSource,result);

        } catch (ParserConfigurationException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (SAXException e) {
            throw new RuntimeException(e);
        } catch (TransformerConfigurationException e) {
            throw new RuntimeException(e);
        } catch (TransformerException e) {
            throw new RuntimeException(e);
        }
    }
}
